package com.cpi.cornpp.purchasedetail.reception.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import KC.ModSegApp.UsuarioApp;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futurepurchase.services.FuturePurchaseService;
import com.cpi.cornpp.instructions.futures.exception.InstructionsFuturesException;
import com.cpi.cornpp.instructions.futures.services.InstructionsFuturesService;
import com.cpi.cornpp.instructions.futures.view.InstructionsFuturesView;
import com.cpi.cornpp.instructionspurchases.exception.InstructionPurchaseException;
import com.cpi.cornpp.instructionspurchases.service.InstructionPurchaseService;
import com.cpi.cornpp.instructionspurchases.view.InstructionPurchaseCreateView;
import com.cpi.cornpp.purchasedetail.reception.form.InstructionPurchaseDetailReceptionForm;
import com.cpi.cornpp.purchaseinstructiondetail.service.InstructionPurchaseDetailService;
import com.cpi.cornpp.purchaseinstructiondetail.view.InstructionPurchaseDetailView;
import com.cpi.cornpp.user.view.UserView;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.NumberUtil;
/**
 * Action Pantalla 
 * Instrucciones de Compra Recepcion
 * @author Edgar Joao
 *
 */
public class InstructionPurchaseDetailReceptionAction extends MappingDispatchAction {
	/** Identifier default workflow*/
	private static final String DEFAULT = "default";//$NON-NLS-1$
	private static final String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String PURCHASE_INSTRUCTION_DETAIL_ID = "purchaseInstructionDetailId";//$NON-NLS-1$
	private static final String PURCHASE_INSTRUCTION_DETAILS = "PURCHASE_INSTRUCTION_DETAILS";//$NON-NLS-1$
	private static final String PURCHASE_INSTRUCTION_ID = "purchaseInstructionId";//$NON-NLS-1$
	private static final String INSTRUCTION_PURCHASE_STATUS_OPENED_ID = "INSTRUCTION_PURCHASE_STATUS_OPENED_ID";//$NON-NLS-1$
	private static final String READ_ONLY = "READ_ONLY";//$NON-NLS-1$
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	private static final String USER_SESSION_NAME = "user";//$NON-NLS-1$
	/**
	 * Metodo que carga la pantalla
	 * de Instrucciones de Compra Recepcion
	 * 
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward go(ActionMapping argMapping, ActionForm argForm, HttpServletRequest argRequest, HttpServletResponse argResponse) throws Exception {
		InstructionPurchaseDetailReceptionForm form = null;
		InstructionPurchaseCreateView view = null;
		InstructionPurchaseService service = null;
		InstructionsFuturesService futureService = null;
		InstructionPurchaseDetailService instructionPurchaseDetailService = null;
		List purchaseInstructionDetailViews = null;
		InstructionPurchaseDetailView instructionPurchaseDetailView = null;
		InstructionPurchaseService instructionPurchaseService = null;
		InstructionPurchaseCreateView instructionPurchaseCreateView = null;
		HttpSession httpSession = null;
		int purchaseInstructionDetailId = 0;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		String purchaseInstructionDetailIdParam = null;
		try{
			httpSession = argRequest.getSession();
			service = new InstructionPurchaseService();
			futureService = new InstructionsFuturesService();
			instructionPurchaseDetailService = new InstructionPurchaseDetailService();
			instructionPurchaseService = new InstructionPurchaseService();
			form = (InstructionPurchaseDetailReceptionForm) argForm;
			purchaseInstructionDetailIdParam = argRequest.getParameter(PURCHASE_INSTRUCTION_DETAIL_ID);
			if( purchaseInstructionDetailIdParam != null ){
				httpSession.setAttribute(PURCHASE_INSTRUCTION_DETAIL_ID, purchaseInstructionDetailIdParam);
			}else{
				purchaseInstructionDetailIdParam = (String)httpSession.getAttribute(PURCHASE_INSTRUCTION_DETAIL_ID);
			}
			purchaseInstructionDetailId = NumberUtil.getInt(purchaseInstructionDetailIdParam);
			instructionPurchaseDetailView = instructionPurchaseDetailService.getById(purchaseInstructionDetailId);
			view = service.getInstructionPurchase(instructionPurchaseDetailView.getPurchaseInstructionId());
			httpSession.setAttribute(PURCHASE_INSTRUCTION_ID, String.valueOf(instructionPurchaseDetailView.getPurchaseInstructionId()));
			purchaseInstructionDetailViews = instructionPurchaseDetailService.list(instructionPurchaseDetailView.getPurchaseInstructionId());
			view.setItemCount(purchaseInstructionDetailViews.size());
			argRequest.setAttribute(PURCHASE_INSTRUCTION_DETAILS , purchaseInstructionDetailViews);
			form.setPurchaseCreateView(view);
			form.setProductList((List)instructionPurchaseDetailView.getProductList());
			form.setAccountPeriod(futureService.periodAccountApplication());
			form.setApplicationPeriod(futureService.periodAccountApplication());
			form.setInstructionsFuturesView(futureService.getInstructionsFuturesList(purchaseInstructionDetailId));
			if(form.getInstructionsFuturesView() != null){
				form.setReceptionFuturesList(fillListReceptionFuturesHelper(form.getInstructionsFuturesView().size()));
			}
			systemParameterCollection = SystemParameterCollection.getInstance();
			systemParameterBean = systemParameterCollection.getSystemParameterBean(INSTRUCTION_PURCHASE_STATUS_OPENED_ID);
			instructionPurchaseCreateView = instructionPurchaseService.getInstructionPurchase(instructionPurchaseDetailView.getPurchaseInstructionId());
			if( instructionPurchaseCreateView.getPurchaseStatus() != systemParameterBean.getIntValue() ){
				argRequest.setAttribute(READ_ONLY, String.valueOf(true));
			}else{
				argRequest.setAttribute(READ_ONLY, String.valueOf(false));
			}
		}catch(InstructionsFuturesException instructionsFuturesException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, instructionsFuturesException);
			throw instructionsFuturesException;
		}catch (InstructionPurchaseException instructionPurchaseException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, instructionPurchaseException);
			throw instructionPurchaseException;
		}catch (Exception e){
			InstructionPurchaseException instructionPurchaseException = null;
			e.printStackTrace(System.out);
			instructionPurchaseException= new InstructionPurchaseException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, instructionPurchaseException);
			throw instructionPurchaseException;
		}
		return argMapping.findForward(DEFAULT);
	}
	/**
	 * Metodo que guarda los futuros comprados
	 * @author Jose Delgado Rguez
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping argMapping, ActionForm argForm,HttpServletRequest argRequest, HttpServletResponse argResponse)throws Exception {
		String forward = "default";
		String messageError = null;
		MessageResources messageResources = null;
		HttpSession httpSession = null;
		FuturePurchaseService futurePurchaseService = null;
		InstructionPurchaseDetailReceptionForm instructionPurchaseDetailReceptionForm = null;
		UserView userView = null;
		UsuarioApp usuarioApp = null;
		try{
			futurePurchaseService = new FuturePurchaseService();
			httpSession = argRequest.getSession();
			messageResources = getResources(argRequest);
			instructionPurchaseDetailReceptionForm = (InstructionPurchaseDetailReceptionForm)argForm;
			userView = (UserView)httpSession.getAttribute(USER_SESSION_NAME);
			usuarioApp = (UsuarioApp)userView.getUserObject();
			futurePurchaseService.insertRealizedFuturePurchase(instructionPurchaseDetailReceptionForm.getPurchaseInstructionDetailId(), instructionPurchaseDetailReceptionForm.getCornContracted(), instructionPurchaseDetailReceptionForm.getCustomerId(), instructionPurchaseDetailReceptionForm.getProductList(), usuarioApp.getLogin(), instructionPurchaseDetailReceptionForm.getReceptionFuturesList());
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (Exception e){
			InstructionPurchaseException instructionPurchaseException = null;
			e.printStackTrace(System.out);
			instructionPurchaseException= new InstructionPurchaseException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, instructionPurchaseException);
			throw instructionPurchaseException;
		}
		return argMapping.findForward(forward);
	}
	/**
	 * Metodo para inicializar el tama�o
	 * de la lista de Recepci�n de Futuros
	 * 
	 * @author Edgar Joao
	 * @param size
	 * @return
	 */
	private List fillListReceptionFuturesHelper(int size){
		InstructionsFuturesView view = null;
		List list = null;
		if(size > 0){
			list = new ArrayList(0);
			for (int i = 0; i < size; i++) {
				view = new InstructionsFuturesView();
				list.add(view);
			}
		}		
		return list;
	}
	
	
}
